package com.example.shardingsphere.config.readwrite;

import com.example.shardingsphere.config.shard.ShardingRuleConfig;
import com.google.common.collect.Lists;
import org.apache.shardingsphere.infra.config.RuleConfiguration;
import org.apache.shardingsphere.readwritesplitting.api.ReadwriteSplittingRuleConfiguration;
import org.apache.shardingsphere.readwritesplitting.api.rule.ReadwriteSplittingDataSourceRuleConfiguration;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

public class DataSourceReadWriteConfig {


    private static ReadwriteSplittingRuleConfiguration initReadWriteDataSource() {


        ReadwriteSplittingDataSourceRuleConfiguration readwriteRuleConfig =
                new ReadwriteSplittingDataSourceRuleConfiguration
                        ("service_order_0", null, "slave_db",
                                Arrays.asList("service_order_1", "service_order_2"), "ROUND_ROBIN");

        // 读写分离配置
        ReadwriteSplittingRuleConfiguration readwriteSplittingRuleConfiguration = new ReadwriteSplittingRuleConfiguration(
                Collections.singleton(readwriteRuleConfig), new HashMap<>());


        return readwriteSplittingRuleConfiguration;
    }

    public static List<RuleConfiguration> getDataSourceConfig(){

        List<RuleConfiguration> ruleConfigurationList= Lists.newArrayList();
        // 读写分离
        ruleConfigurationList.add(initReadWriteDataSource());

        // 分库分表
        ruleConfigurationList.add(ShardingRuleConfig.initShardingRuleConfiguration());

        return ruleConfigurationList;
    }
}
